package com.appview;

import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.Student;
import com.model.Teacher;
import com.util.Config;
import sun.awt.ConstrainableGraphics;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by QiHuang on 2015/10/27.
 * 密码修改界面
 */
public class PswModify extends JFrame implements ActionListener {
    private JLabel jLabel, jLabel2, jLabel3, jLabel4;
    private JPanel jPanel, jPanel2, jPanel3, jPanel4, jp_button;
    private JButton jButton1, jButton2;
    private JPasswordField jpf2, jpf3, jpf4;
    private String accout;
    public PswModify(String accout) {
        this.accout = accout;
    }

    public PswModify() {

    }

    public void openView() {
        initComponent();
        jPanel.add(jLabel, new FlowLayout(FlowLayout.CENTER));
        jPanel2.add(jLabel2);
        jPanel2.add(jpf2);
        jPanel3.add(jLabel3);
        jPanel3.add(jpf3);
        jPanel4.add(jLabel4);
        jPanel4.add(jpf4);
        jp_button.add(jButton1);
        jp_button.add(jButton2);

        add(jPanel);
        add(jPanel2);
        add(jPanel3);
        add(jPanel4);
        add(jp_button);
        setLayout(new GridLayout(5, 1));
        setSize(400, 200);
        setVisible(true);
        setLocation(400, 200);
        setResizable(false);
        setTitle("密码修改");
        setFocusable(true);
        setAlwaysOnTop(true);
    }

    public void initComponent() {
        jLabel = new JLabel("密码操作");
        jLabel2 = new JLabel("原始密码：");
        jLabel3 = new JLabel("新的密码：");
        jLabel4 = new JLabel("确认密码：");

        jpf2 = new JPasswordField(10);
        jpf3 = new JPasswordField(10);
        jpf4 = new JPasswordField(10);

        jPanel = new JPanel();
        jPanel2 = new JPanel();
        jPanel3 = new JPanel();
        jPanel4 = new JPanel();
        jp_button = new JPanel();

        jButton1 = new JButton("确认");
        jButton2 = new JButton("取消");

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            //确认修改
            //判断两次密码是否一致
            String p1 = jpf3.getText();
            String p2 = jpf4.getText();
            if (p1.equals(p2)) {
                if (checkPwd()) {
                    JOptionPane.showMessageDialog(this, "修改成功！", "Tip", JOptionPane.DEFAULT_OPTION);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "密码错误！", "信息有误", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "两次输入的密码不一致，请重新输入！", "信息有误", JOptionPane.WARNING_MESSAGE);
            }

        }else {
            this.dispose();
        }
    }


    private boolean checkPwd() {
        String pwd = jpf2.getText().toString();
        String newPwd = jpf3.getText().toString();
        TeacherDao teacherDao = TeacherDao.getInstance();
        StudentDao studentDao = StudentDao.getInstance();
        if (Config.getLoginType() == 0) {
            //教师
            Teacher teacher = teacherDao.load(accout);
            if (pwd.equals(teacher.getPassword().trim())) {
                teacher.setPassword(newPwd);
                teacherDao.update(teacher);
                return true;
            } else {
                return false;
            }
        } else if (Config.getLoginType() == 1) {
            Student student = studentDao.loadByAccount(accout);
            if (pwd.equals(student.getPassword().trim())) {
                student.setPassword(newPwd);
                studentDao.update(student);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
}
